package com.ivo.biz;


import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.ivo.core.WebConstants;
import com.ivo.quartz.AryPerformanceQuartz;
import com.ivo.sys.user.User;
import com.ivo.util.JsonUtil;


@RestController
@RequestMapping("/ary/performance")
public class AryPerformanceController {
	
	@Autowired
	private PerformanceService performanceService;
	
	@Autowired
	private AryPerformanceQuartz aryPerformanceQuartz;
	
	
	@RequestMapping(value = "/aryPerformance")
	@ResponseBody
	public ModelAndView getPerformance(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("sys/ary");
		mv.addObject("category","aryPerformance");
		return mv;
		
	}
	
	@RequestMapping(value = "/aryDustFree")
	@ResponseBody
	public ModelAndView getDustFree(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("sys/ary");
		mv.addObject("category","dustfree");
		return mv;
		
	}
	
	
	@RequestMapping(value = "/aryAttendenceThisMon")
	@ResponseBody
	public ModelAndView getAttendence(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("sys/ary");
		mv.addObject("category","aryAttendenceThisMon");
		return mv;
		
	}
	
	
	@RequestMapping(value = "/aryWorkType")
	@ResponseBody
	public ModelAndView getAryWorkType(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("sys/ary");
		mv.addObject("category","aryWorkType");
		return mv;
		
	}
	
	
	@RequestMapping(value = "/aryWorkAbility")
	@ResponseBody
	public ModelAndView getAryWorkAbility(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("sys/ary");
		mv.addObject("category","aryWorkAbility");
		return mv;
		
	}
	
	
	@RequestMapping(value = "/aryCompAtibility")
	@ResponseBody
	public ModelAndView getAryCompAtibility(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("sys/ary");
		mv.addObject("category","aryCompAtibility");
		return mv;
		
	}
	
	
	@RequestMapping(value = "/aryMajorLoss")
	@ResponseBody
	public ModelAndView getAryMajorLoss(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("sys/ary");
		mv.addObject("category","aryMajorLoss");
		return mv;
		
	}

	
	
	@RequestMapping(value = "/majorAddition")
	@ResponseBody
	public ModelAndView getMajorAddition(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("sys/ary");
		mv.addObject("category","majorAddition");
		return mv;
		
	}
	
	
	
	@RequestMapping(value = "/list")
	@ResponseBody
	public String getList(
    		HttpServletRequest request,
    		@RequestParam(value = "allFlag", required = false) int allFlag,
    		@RequestParam(value = "pno", required = false) int pno,
    		@RequestParam(value = "emp_id", required = false) String emp_id,
    		@RequestParam(value = "month", required = false) String month,
    		@RequestParam(value = "score", required = false) String score,
    		@RequestParam(value = "classtype", required = false) String classtype,
    		@RequestParam(value = "grade", required = false) String grade,
    		@RequestParam(value = "ope_fk", required = false) String ope_id,
    		@RequestParam(value = "bonus", required = false) String bonus
    		) throws ParseException{
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result", performanceService.getARYPerformance(emp_id,month,ope_id,score,pno,allFlag,bonus,grade,classtype));
        return JsonUtil.toJson(returnMap);
    }
	
	
	@RequestMapping(value = "/getListAttendenceThisMon")
	@ResponseBody
	public String getListAttendenceThisMon(
    		HttpServletRequest request,
    		@RequestParam(value = "allFlag", required = false) int allFlag,
    		@RequestParam(value = "pno", required = false) int pno,
    		@RequestParam(value = "emp_id", required = false) String emp_id,
    		@RequestParam(value = "month", required = false) String month,
    		@RequestParam(value = "classtype", required = false) String classtype,
    		@RequestParam(value = "ope_id", required = false) String ope_id
    		) throws ParseException{
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result", performanceService.getListAryAttendenceThisMon(emp_id,month,ope_id,pno,allFlag,classtype));
        return JsonUtil.toJson(returnMap);
    }
	
	
	@RequestMapping(value = "/getListWorkType")
	@ResponseBody
	public String getListWorkType(
    		HttpServletRequest request,
    		@RequestParam(value = "allFlag", required = false) int allFlag,
    		@RequestParam(value = "pno", required = false) int pno,
    		@RequestParam(value = "classtype", required = false) String classtype,
    		@RequestParam(value = "emp_id", required = false) String emp_id,
    		@RequestParam(value = "month", required = false) String month,
    		@RequestParam(value = "ope_id", required = false) String ope_id
    		) throws ParseException{
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result", performanceService.getListWorkType(emp_id,month,ope_id,pno,allFlag,classtype));
        return JsonUtil.toJson(returnMap);
    }
	
	
	@RequestMapping(value = "/getListWorkAbility")
	@ResponseBody
	public String getListWorkAbility(
    		HttpServletRequest request,
    		@RequestParam(value = "allFlag", required = false) int allFlag,
    		@RequestParam(value = "pno", required = false) int pno,
    		@RequestParam(value = "emp_id", required = false) String emp_id,
    		@RequestParam(value = "classtype", required = false) String classtype,
    		@RequestParam(value = "month", required = false) String month,
    		@RequestParam(value = "ope_id", required = false) String ope_id
    		) throws ParseException{
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result", performanceService.getListWorkAbility(emp_id,month,ope_id,pno,allFlag,classtype));
        return JsonUtil.toJson(returnMap);
    }
	
	
	
	@RequestMapping(value = "/getListCompAtibility")
	@ResponseBody
	public String getListCompAtibility(
    		HttpServletRequest request,
    		@RequestParam(value = "allFlag", required = false) int allFlag,
    		@RequestParam(value = "pno", required = false) int pno,
    		@RequestParam(value = "emp_id", required = false) String emp_id,
    		@RequestParam(value = "classtype", required = false) String classtype,
    		@RequestParam(value = "month", required = false) String month,
    		@RequestParam(value = "ope_id", required = false) String ope_id
    		) throws ParseException{
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result", performanceService.getListCompAtibility(emp_id,month,ope_id,pno,allFlag,classtype));
        return JsonUtil.toJson(returnMap);
    }
	
	
	
	@RequestMapping(value = "/getListMajorLoss")
	@ResponseBody
	public String getListMajorLoss(
    		HttpServletRequest request,
    		@RequestParam(value = "allFlag", required = false) int allFlag,
    		@RequestParam(value = "pno", required = false) int pno,
    		@RequestParam(value = "from_date", required = false) String from_date,
    		@RequestParam(value = "to_date", required = false) String to_date,
    		@RequestParam(value = "classtype", required = false) String classtype,
    		@RequestParam(value = "type", required = false) String type,
    		@RequestParam(value = "ope_id", required = false) String ope_id
    		) throws ParseException{
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result", performanceService.getListMajorLoss(from_date,to_date,type,ope_id,pno,allFlag,classtype));
        return JsonUtil.toJson(returnMap);
    }
	

	
	
	@RequestMapping(value = "/updateWorkability", method = RequestMethod.POST)
    @ResponseBody
    public String updateAryWorkability(HttpServletRequest request,
    		@RequestParam(value = "month") String month,
    		@RequestParam(value = "emp_id") String emp_id,
    		@RequestParam(value = "workability") String workability
    		) {
    	String err_code = "";
    	String err_msg = "";
    	String user_id = "";
    	User user = (User) request.getSession().getAttribute(WebConstants.SessionUser);
    	if(user!=null) user_id = user.getUser_id();
    	String result = performanceService.updateAryWorkability(month,emp_id,user_id,workability);
    	if(result.equals("1")){
    		err_code = "1";
    		err_msg = "员工"+emp_id+"不存在！";
    	}else if(result.equals("0")){
    		err_code = "0";
    		err_msg = "success";
    	}
    	
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", err_code);
        returnMap.put("err_msg", err_msg);
        return JsonUtil.toJson(returnMap);
    }
	
	
	
	@RequestMapping(value = "/getAverageBouns")
    @ResponseBody
    public String getAverageBouns(
    		@RequestParam(value = "month", required = false) String month,
    		@RequestParam(value = "ope_fk", required = false) Long ope_fk,
    		@RequestParam(value = "bonus", required = false) String bonus,
    		@RequestParam(value = "score", required = false) String score,
    		@RequestParam(value = "emp_id", required = false) String emp_id,
    		@RequestParam(value = "classtype", required = false) String classtype,
    		@RequestParam(value = "grade", required = false) String grade
    		) {
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result", performanceService.getAryAverageBouns(month,ope_fk,bonus,score,emp_id,grade,classtype));
        String s =  JsonUtil.toJson(returnMap);
        return JsonUtil.toJson(returnMap);
    }
	
	@RequestMapping(value = "/getAllBonuses")
    @ResponseBody
    public String getAllBonuses(
    		@RequestParam(value = "month", required = false) String month,
    		@RequestParam(value = "ope_fk", required = false) Long ope_fk,
    		@RequestParam(value = "bonus", required = false) String bonus,
    		@RequestParam(value = "score", required = false) String score,
    		@RequestParam(value = "emp_id", required = false) String emp_id,
    		@RequestParam(value = "classtype", required = false) String classtype,
    		@RequestParam(value = "grade", required = false) String grade
    		) {
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result",performanceService.getAryAllBonuses(month,ope_fk,bonus,score,emp_id,grade,classtype));
        String s =  JsonUtil.toJson(returnMap);
        return JsonUtil.toJson(returnMap);
    }
	
	@RequestMapping(value = "/getActualBonus")
    @ResponseBody
    public String getActualBonus(
    		@RequestParam(value = "month", required = false) String month,
    		@RequestParam(value = "ope_fk", required = false) Long ope_fk,
    		@RequestParam(value = "bonus", required = false) String bonus,
    		@RequestParam(value = "score", required = false) String score,
    		@RequestParam(value = "emp_id", required = false) String emp_id,
    		@RequestParam(value = "classtype", required = false) String classtype,
    		@RequestParam(value = "grade", required = false) String grade
    		) {
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result",performanceService.getAryActualBonus(month,ope_fk,bonus,score,emp_id,grade,classtype));
        String s =  JsonUtil.toJson(returnMap);
        return JsonUtil.toJson(returnMap);
    }
	
	
	@RequestMapping(value = "/createByDay", method = RequestMethod.POST)
    @ResponseBody
    public String createByDay(HttpServletRequest request,
            @RequestParam(value = "fabDate") String fabDate,
            @RequestParam(value = "type") Long type,
            @RequestParam(value = "ope_id_ARY") Long ope_id,
            @RequestParam(value = "emp_id") String emp_id,
            @RequestParam(value = "emp_name") String emp_name,
            @RequestParam(value = "reason") String reason
    		) throws ParseException {
    	
    	String err_code = "";
    	String err_msg = "";
    	String data = performanceService.createMajorLoss(fabDate,type,ope_id,emp_id,emp_name,reason);
    	if(!data.equals("0")){
    		err_code = "0";
    		err_msg = "success";
    	}
    	else {
    		err_code = "1";
    		err_msg = "create failed";
    	}
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", err_code);
        returnMap.put("err_msg", err_msg);
        return JsonUtil.toJson(returnMap);
    }
	
	
    @RequestMapping(value = "/deleteByDay")
    public String delete(@RequestParam(value = "id") Long id) {
    	String err_code = "";
    	String err_msg = "";
    	int r = performanceService.deleteByDay(id);
    	if(r==1){
    		err_code = "0";
    		err_msg = "success";
    	}
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", err_code);
        returnMap.put("err_msg", err_msg);
        return JsonUtil.toJson(returnMap);
    }
	
	
	@RequestMapping(value = "/syschron", method = RequestMethod.POST)
    @ResponseBody
    public String syschron(HttpServletRequest request) {
		aryPerformanceQuartz.execute();
		aryPerformanceQuartz.Calculate();
		aryPerformanceQuartz.CalculateBonus();
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        return JsonUtil.toJson(returnMap);
    }
	
	
	
    
	@RequestMapping(value = "/updateCompatibility", method = RequestMethod.POST)
    @ResponseBody
    public String updateAryCompatibility(HttpServletRequest request,
    		@RequestParam(value = "month") String month,
    		@RequestParam(value = "emp_id") String emp_id,
    		@RequestParam(value = "compatibility") String compatibility
    		) {
    	String err_code = "";
    	String err_msg = "";
    	String user_id ="";
    	User user = (User) request.getSession().getAttribute(WebConstants.SessionUser);
    	if(user!=null) user_id = user.getUser_id();
    	String result = performanceService.updateAryCompatibility(month,emp_id,user_id,compatibility);
    	if(result.equals("1")){
    		err_code = "1";
    		err_msg = "员工"+emp_id+"不存在！";
    	}else if(result.equals("0")){
    		err_code = "0";
    		err_msg = "success";
    	}
    	
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", err_code);
        returnMap.put("err_msg", err_msg);
        return JsonUtil.toJson(returnMap);
    }
    
		
	
}
